package ntd.util.frame.spring.mvc;

import lombok.extern.slf4j.Slf4j;
import ntd.util.config.common.enums.BaseEnumInterface;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PersonConverter implements Converter<String, BaseEnumInterface> {

    @Override
    public BaseEnumInterface convert(String value) {
        log.info("参数是: {}", value);
        return (BaseEnumInterface) PersonConverter.getEnum(BaseEnumInterface.class, value);
    }


    public static <T extends BaseEnumInterface> Object getEnum(Class<T> targerType, String source) {
        for (T enumObj : targerType.getEnumConstants()) {
            if (source.equals(String.valueOf(enumObj.getName()))) {
                return enumObj;
            }
        }
        return null;
    }

}